<template>
  <div>
    <top-nav-bar :applyPageRoute="nowRouteTitle" @back="back"></top-nav-bar>
    <a-card style="position:relative; border:none;">
      <a-row>
        <a-row class="textAlignCenter">
          <a-col :span="3" class="textAlignCenter" style="padding-left:50px;">
            <div class="ant-upload-preview">
              <img :src="form.head_portrait.url" v-if="form.head_portrait" />
              <img src="~@/assets/defaultHeadimg.png" v-else />
            </div>
          </a-col>
          <a-col :span="16" style="text-align:left;">
            <a-row style="text-align:left;">
              <a-col :span="9" style="text-align:left;">
                <div
                  style="font-size: 16px;font-weight: 700;height: 30px;line-height: 30px;"
                >{{form.name}}</div>
                <div style="height: 30px;line-height: 30px;">{{form.phone}}</div>
              </a-col>
              <a-col :span="15" style="text-align:left;">
                <div>
                  擅长科目：
                  <span
                    style="font-weight: 700;height: 30px;line-height: 30px;"
                  >{{form.subject_names||'--'}}</span>
                </div>
              </a-col>
            </a-row>
          </a-col>
        </a-row>
      </a-row>
      <a-button
        type="primary"
        style="position:absolute;right:20px;top:20px;"
        @click="edit(Number($route.query.id))"
      >编辑教师资料</a-button>
    </a-card>
    <a-card style="margin-top:20px; border:none;" :body-style="{padding: '6px 16px'}">
      <a-tabs defaultActiveKey="1">
        <a-tab-pane tab="当前所授班级" key="1">
          <teach-class :id="Number(this.$route.query.id)"></teach-class>
        </a-tab-pane>
        <a-tab-pane tab="上课记录" key="2">
          <class-record :id="Number(this.$route.query.id)"></class-record>
        </a-tab-pane>
      </a-tabs>
    </a-card>
    <upload-avatar ref="modal" @ok="setavatar" />
    <subject-model ref="subj_model" @reload="reloadSelect" />
    <user-info-model ref="editUser" @reload="getInfo()" @closeLoading="loading=false" :type="1"></user-info-model>
  </div>
</template>

<script>
import { subjectList } from '@/api/subject'
import uploadAvatar from '@/components/uploadAvatar'
import topNavBar from '@/components/topNavBar/topNavBar'
import { STable } from '@/components'
import subjectModel from '@/components/subjectsModel'
import classRecord from './child/classRecord'
import teachClass from './child/teachClass'
import { accountEdit, roleList, accountInfo } from '@/api/account'
import { changeTeacherType, teacherEdit, teacherInfo } from '@/api/teacher'
import userInfoModel from '@/components/editUserInfo'
export default {
  data() {
    return {
      img: '',
      visible: false,
      loading: false,
      layout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 14 }
      },
      confirmLoading: false,
      form: {},
      subjectList: [],
      rules: {
        name: [{ required: true, message: '请输入教师姓名' }],
        phone: [{ required: true, pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码！' }]
      }
    }
  },
  components: {
    STable,
    uploadAvatar,
    subjectModel,
    classRecord,
    teachClass,
    topNavBar,
    userInfoModel
  },
  methods: {
    setavatar(url) {
      teacherEdit(
        {
          head_portrait: url,
          name: this.form.name,
          phone: this.form.phone
        },
        Number(this.$route.query.id)
      )
        .then(res => {
          this.$message.success('设置头像成功')
          this.editVisibles = false
          this.getInfo()
        })
        .catch(err => {
          this.$message.success('设置头像失败')
        })
    },
    edit(id) {
      this.loading = true
      accountInfo({ school_id: this.$store.state.user.school }, this.form.user_id)
        .then(res => {
          let record = new Object()
          this.mdl = Object.assign({}, res)
          record.id = this.mdl.id
          record.name = this.mdl.name
          record.sex = this.mdl.sex ? this.mdl.sex : 1
          record.phone = this.mdl.phone
          record.subject_ids = this.mdl.subject_id
          record.structures_ids = this.mdl.department_ids
          record.role_ids = this.mdl.roleID
          record.remark = this.mdl.remark
          record.is_teaching = this.mdl.is_teaching
          this.$refs.editUser.showVisible(record)
        })
        .catch(err => {
          this.$message.error('获取教师详情失败')
        })
    },
    handleOk(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let data = JSON.parse(JSON.stringify(this.form))
          teacherEdit(
            {
              name: data.name,
              sex: data.sex ? data.sex : 1,
              remarks: data.remarks,
              phone: data.phone,
              subject_ids: data.subject_ids
            },
            data.id
          )
            .then(res => {
              this.$message.success('修改成功')
              this.getInfo()
              this.visible = false
            })
            .catch(err => {
              this.$message.error(err.data.message || '修改失败')
            })
        } else {
          return false
        }
      })
    },
    getInfo() {
      teacherInfo(Number(this.$route.query.id))
        .then(res => {
          this.form = res
        })
        .catch(err => {
          this.$message.error(err.data.message || '获取教师信息失败')
        })
    },
    showChild() {
      this.$refs.subj_model.showModal()
    },
    reloadSelect() {
      subjectList({ pageSize: 999999 }).then(res => {
        this.subjectList = res.data
      })
    },
    back() {
      this.$router.go(-1)
    }
  },
  created() {
    this.nowRouteTitle = this.$route.meta.title
    this.getInfo()
    this.reloadSelect()
  }
}
</script>

<style lang="less" scoped>
.textAlignCenter {
  text-align: center;
}
.ant-upload-preview {
  position: relative;
  margin-left: 28px;
  width: 100%;
  max-width: 64px;
  margin-bottom: 20px;
  border-radius: 50%;
  box-shadow: 0 0 4px #ccc;

  .upload-icon {
    position: absolute;
    top: 0;
    right: 10px;
    font-size: 1rem;
    // padding: 0.5rem;
    background: rgba(222, 221, 221, 0.7);
    border-radius: 50%;
    border: 1px solid rgba(0, 0, 0, 0.2);
  }
  .mask {
    opacity: 0;
    position: absolute;
    background: rgba(0, 0, 0, 0.4);
    cursor: pointer;
    transition: opacity 0.4s;

    &:hover {
      opacity: 1;
    }

    i {
      font-size: 1rem;
      position: absolute;
      top: 50%;
      left: 50%;
      margin-left: -0.5rem;
      margin-top: -0.5rem;
      color: #d6d6d6;
    }
  }

  img,
  .mask {
    width: 100%;
    max-width: 64px;
    height: 64px;
    border-radius: 50%;
    overflow: hidden;
  }
}
</style>